﻿using System;
using System.Text;
using System.IO;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using XZL.Web.Da;
using XZL.Web.Entity;

namespace XZL.Web.Biz
{
    public class UserBiz
    {
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="referrerID"></param>
        /// <param name="weixinID"></param>
        /// <param name="weixinName"></param>
        /// <param name="pwd"></param>
        /// <param name="mobile"></param>
        /// <returns>-1:微信号重复   -2:手机号重复  0:注册成功</returns>
        public static int Register(string userID, string referrerID, string weixinID, string weixinName, string nickName, string headImg, string pwd, string mobile)
        {
            UserDBDataContext db = new UserDBDataContext();
            int res = db.Proc_UserInfo_Register(userID, referrerID, weixinID, weixinName, nickName, headImg, pwd, mobile);
            return res;
        }

        /// <summary>
        /// 短信接口链接
        /// </summary>
        public static string PostUrl = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        /// <summary>
        /// 发送手机短信
        /// </summary>
        /// <param name="num"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static int SendMobileCode(string mobile, string msg)
        {
            string account = "cf_xuezhilang";
            string password = "xzl123";//密码可以使用明文密码或使用32位MD5加密
            string content = msg;

            string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";

            UTF8Encoding encoding = new UTF8Encoding();
            byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postData.Length;

            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// 保存短信验证码
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool UpdateMobileCode(string phone, string msg)
        {
            UserDBDataContext db = new UserDBDataContext();
            db.Proc_UserMobileCode_UpdateCode(phone, msg);
            return true;
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="phone"></param>
        public static string GetMobileCode(string phone)
        {
            UserDBDataContext db = new UserDBDataContext();
            Proc_UserMobileCode_SelectCodeInfoResult tmp = db.Proc_UserMobileCode_SelectCodeInfo(phone).FirstOrDefault();
            if (tmp != null)
            {
                return tmp.CodeContent;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 普通登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static UserInfo LoginByPhone(string phone, string pwd)
        {
            UserDBDataContext db = new UserDBDataContext();
            Proc_UserInfo_LoginByPhoneResult obj = db.Proc_UserInfo_LoginByPhone(phone, pwd).FirstOrDefault();
            if (obj != null)
            {
                UserInfo u = new UserInfo();
                u.Account = obj.Account;
                u.CreateTime = Convert.ToDateTime(obj.CreateTime);
                u.HeadPortrait = obj.HeadPortrait;
                u.MobilePhone = obj.MobilePhone;
                u.NickName = obj.NickName;
                u.Password = obj.Password;
                u.ReferrerID = obj.ReferrerID;
                u.ReferrerName = obj.ReferrerName;
                u.ReferrerPhone = obj.RefferPhone;
                u.UserID = obj.UserID;
                u.UserName = obj.UserName;
                u.UserNum = obj.UserNum;
                u.UserTypeID = obj.UserType;
                u.UserTypeName = obj.UserTypeName;
                u.WeiXinID = obj.WeiXinID;
                u.WeiXinName = obj.WeiXinName;
                u.Status = obj.Status;

                return u;
            }

            return null;
        }

        /// <summary>
        /// 验证码登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static UserInfo LoginByCode(string phone, string code)
        {
            UserDBDataContext db = new UserDBDataContext();
            Proc_UserInfo_LoginByCodeResult obj = db.Proc_UserInfo_LoginByCode(phone, code).FirstOrDefault();
            if (obj != null)
            {
                UserInfo u = new UserInfo();
                u.Account = obj.Account;
                u.CreateTime = Convert.ToDateTime(obj.CreateTime);
                u.HeadPortrait = obj.HeadPortrait;
                u.MobilePhone = obj.MobilePhone;
                u.NickName = obj.NickName;
                u.Password = obj.Password;
                u.ReferrerID = obj.ReferrerID;
                u.ReferrerName = obj.ReferrerName;
                u.ReferrerPhone = obj.RefferPhone;
                u.UserID = obj.UserID;
                u.UserName = obj.UserName;
                u.UserNum = obj.UserNum;
                u.UserTypeID = obj.UserType;
                u.UserTypeName = obj.UserTypeName;
                u.WeiXinID = obj.WeiXinID;
                u.WeiXinName = obj.WeiXinName;
                u.Status = obj.Status;

                return u;
            }

            return null;
        }

        /// <summary>
        /// 微信登录
        /// </summary>
        /// <param name="weiXinID"></param>
        /// <returns></returns>
        public static UserInfo LoginByWeiXin(string weiXinID, string weiXinNick, string headImg)
        {
            UserDBDataContext db = new UserDBDataContext();
            Proc_UserInfo_LoginByWeiXinResult obj = db.Proc_UserInfo_LoginByWeiXin(weiXinID, weiXinNick, headImg).FirstOrDefault();
            if (obj != null)
            {
                UserInfo u = new UserInfo();
                u.Account = obj.Account;
                u.CreateTime = Convert.ToDateTime(obj.CreateTime);
                u.HeadPortrait = obj.HeadPortrait;
                u.MobilePhone = obj.MobilePhone;
                u.NickName = obj.NickName;
                u.Password = obj.Password;
                u.ReferrerID = obj.ReferrerID;
                u.ReferrerName = obj.ReferrerName;
                u.ReferrerPhone = obj.RefferPhone;
                u.UserID = obj.UserID;
                u.UserName = obj.UserName;
                u.UserNum = obj.UserNum;
                u.UserTypeID = obj.UserType;
                u.UserTypeName = obj.UserTypeName;
                u.WeiXinID = obj.WeiXinID;
                u.WeiXinName = obj.WeiXinName;
                u.Status = obj.Status;

                return u;
            }

            return null;
        }

        /// <summary>
        /// 获取用户信息[by UserID]
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static Proc_UserInfo_SelectByUserIDResult GetUserInfoByUserID(string userID)
        {
            UserDBDataContext db = new UserDBDataContext();
            return db.Proc_UserInfo_SelectByUserID(userID).FirstOrDefault();
        }

        /// <summary>
        /// 获取用户信息[by UserName]
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static Proc_UserInfo_SelectByUserNumResult GetUserInfoByUserNum(int userNum)
        {
            UserDBDataContext db = new UserDBDataContext();
            return db.Proc_UserInfo_SelectByUserNum(userNum).FirstOrDefault();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="type"></param>
        /// <param name="code"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        public static int UpdatePwd(string userID, string code, string newPwd)
        {
            UserDBDataContext db = new UserDBDataContext();
            return db.Proc_UserInfo_UpdatePWD(userID, code, newPwd);
        }

        /// <summary>
        /// 提交申请
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="type"></param>
        /// <param name="reson"></param>
        /// <param name="phone"></param>
        /// <param name="realName"></param>
        /// <param name="city"></param>
        /// <param name="idCard"></param>
        /// <param name="address"></param>
        /// <param name="company"></param>
        /// <param name="position"></param>
        /// <param name="contentTime"></param>
        /// <returns></returns>
        public static bool AddApplyInfo(string userID, int type, string reson, string phone, string realName, string city, string idCard, string address, string company, string position, string contentTime)
        {
            UserDBDataContext db = new UserDBDataContext();
            db.Proc_ApplyInfo_Addition(userID, type, reson, phone, realName, city, idCard, address, company, position, contentTime);
            return true;
        }

        /// <summary>
        /// 获取个人消费
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Proc_AccountLog_UserExpenseResult> GetUserExpense(string userID, DateTime start, DateTime end, ref double? count)
        {
            UserDBDataContext db = new UserDBDataContext();
            return db.Proc_AccountLog_UserExpense(userID, start, end, ref count).ToList();
        }

        /// <summary>
        /// 获取推广消费
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Proc_AccountLog_PromoteExpenseResult> GetPromoteExpense(string userID, DateTime start, DateTime end, ref double? amount, ref int? userCount)
        {
            UserDBDataContext db = new UserDBDataContext();
            return db.Proc_AccountLog_PromoteExpense(userID, start, end, ref amount, ref userCount).ToList();
        }

        /// <summary>
        /// 获取小组消费
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Proc_AccountLog_TeamExpenseResult> GetTeamExpense(string userID, DateTime start, DateTime end, ref double? amount, ref int? userCount)
        {
            UserDBDataContext db = new UserDBDataContext();
            return db.Proc_AccountLog_TeamExpense(userID, start, end, ref amount, ref userCount).ToList();
        }
    }
}
